import {ChangeDetectorRef, Component, ViewChild} from '@angular/core';
import {Content, Events, IonicPage, NavController, NavParams} from 'ionic-angular';
import {LoadingService} from "../../../../services/common/loading.service";
import {LiveShowSituationProvider} from "../../../../providers/my-team/live-show-situation/live-show-situation.provider";
import {LiveShowModel} from "../../../../model/pages/my-team/live-show/live-show-situation.model";
import {DateService} from "../../../../services/common/date.service";
import {LiveShowFilterPage} from "../live-show-filter/live-show-filter";
import {ActivityStatus} from "../../../../model/pages/activity/activity-filter/activity-filter.model";
import * as _ from 'lodash';
import {AdpActionSheetController} from "../../../../components/common/adp-action-sheet/adp-action-sheet-controller";
/**
 *直播情况
 */

@IonicPage()
@Component({
    selector: 'page-live-show-situation',
    templateUrl: 'live-show-situation.html',
    providers: [
        LiveShowSituationProvider,
        DateService,
    ]
})
export class LiveShowSituationPage {

    isScrolling = false;
    isLoaded: boolean = false;
    groupName;
    livShowData: LiveShowModel = new LiveShowModel();

    /**
     * 附加筛选条件变量（用作展示）
     */
    private tempFilter = {
        classify: 0,
        groupId: '',
        status:  [1, 2],
        startTime: this.dateService.getCurrentMonthFirstDay(),
        endTime: this.dateService.getCurrentMonthLastDay(),
    };

    @ViewChild(Content) content: Content;

    constructor(public navCtrl: NavController,
                private events: Events,
                public navParams: NavParams,
                private liveProvider: LiveShowSituationProvider,
                public load: LoadingService,
                public dateService:DateService,
                private ref: ChangeDetectorRef,
                public adpActionSheet: AdpActionSheetController) {
        this.getLiveShowList();
    }


    ionViewDidLoad() {
        this.receiveFilterEvent();
    }

    ionViewWillUnload(){
        this.events.unsubscribe('liveShow:search');
    }

    isData;
    errorData;
    /**
     * 获取直播情况列表
     */
    getLiveShowList(id = '') {
        this.isLoaded = false;
        this.load.showLoading();

        this.liveProvider.getLiveShowList(id).subscribe((res) => {
            this.livShowData = JSON.parse(res);
            this.livShowData.groupList.unshift({'name':'所有组','id':''});

            //this.livShowData.webcastList = []
            this.isLoaded = true;
            this.load.dismissLoading();
            this.ref.detectChanges();
        }, error => {
            this.load.dismissLoading();
            this.errorData = JSON.parse(error);
            this.isData = true;
        });
    }
    private currentGroupId='';
    private currentGroupName='所有组';
    /**
     * 打开小组
     */

    openSelect(){
        let jobArray = [];

        this.livShowData.groupList.forEach((item)=>{
            let obj = {};
            obj['text']=item.name;
            obj['cssClass'] = this.currentGroupName == item.name?'action-sheet-selected':'';
            obj['value']=item.id;
            obj['handler'] = ()=>{
                this.currentGroupName = item.name;
                console.log(item.name,'123');
                this.chooseGroup(item.id);
            };
            jobArray.push(obj);
        });
        this.adpActionSheet.create({
            title: '选择组',
            buttons: jobArray,
            cssClass:'mySelect'
        }).present();
    }
    /*openSelect(){

        let jobArray = [];

        for(let m in this.livShowData.groupList){
            let obj = {};
            obj['text']=this.livShowData.groupList[m].name;
            obj['cssClass'] = this.currentGroupName == this.livShowData.groupList[m].name?'action-sheet-selected':'';
            let value = this.livShowData.groupList[m].name;
            obj['handler'] = ()=>{
                this.currentGroupName = value;
                // console.log(this.livShowData.groupList[m].name,'123');
                // this.chooseGroup(this.livShowData.groupList[m].id);
            };
            jobArray.push(obj);
            console.log(jobArray)
        }
        this.adpActionSheet.create({
            title: '选择组',
            buttons: jobArray,
            cssClass:'mySelect'
        }).present();
    }*/
    /**
     * 选择小组
     */
    chooseGroup(item) {
        this.currentGroupId = item;
        this.getLiveShowList(item);

    }


    /**
     * 回到顶部
     */
    goUp() {
        if (this.isScrolling) {
            setTimeout(() => {
                this.goUp();
            }, 300);
            return;
        }
        this.content.scrollToTop();
    }

    scrollStart(e) {
        this.isScrolling = true;
    }

    scrollEnd(e) {
        this.isScrolling = false;
    }

    /**
     * 跳转到直播情况详情
     */
    goToLiveShowDetail(id, name, time) {

        this.navCtrl.push('LiveShowDetailPage', {
            'id': id,
            'name': name,
            'time': time,
            'groupId':this.currentGroupId
        });
    }

    /**
     * 接收筛选条件
     */
    receiveFilterEvent() {
        let self = this;
        this.events.subscribe('liveShow:search',
            (filter) => {
                self.tempFilter = filter;
                let cloneFilter = _.cloneDeep(filter);
                if (cloneFilter.status.length === 0) {
                    cloneFilter.status = [ActivityStatus.notStart, ActivityStatus.processing, ActivityStatus.over];
                }
                this.getLiveShowList();
            });
    }


    /**
     * 返回
     * @param ev
     */
    backButtonClick(ev) {
        ev.preventDefault();
        ev.stopPropagation();
        this.navCtrl && this.navCtrl.pop(null, null);
    }

    toFilterPage() {
        this.navCtrl.push('LiveShowFilterPage', {
            filter: this.tempFilter
        });
    }
}
